public class LeetCode {
    public int i = 0;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        i = postorder.length - 1;
        return buildTreeChild(postorder,inorder,0,postorder.length-1);
    }
    public TreeNode buildTreeChild(int[] postorder,int[] inorder,int inbegin, int inend){
        if(inbegin > inend){
            return null;
        }
        TreeNode root = new TreeNode(postorder[i]);
        //找到当前根，在中序遍历的位置
        int rootindex = findindex(inorder,inbegin,inend,postorder[i]);
        i--;
        root.right = buildTreeChild(postorder,inorder,rootindex+1,inend);
        root.left = buildTreeChild(postorder,inorder,inbegin,rootindex-1);
        return root;
    }
    private int findindex(int[] inorder,int inbegin,int inend,int key){
        for( int j = inbegin; j <= inend;j++){
            if(inorder[j] == key){
                return j;
            }
        }
        return -1;
    }
}
